﻿using System.Collections.Generic;
using System.Linq;
using QFramework;
using UnityEngine;

namespace ProjectMyFarmAndShe
{
    public interface IToolbarSystem : ISystem
    {
        List<ToolbarData> ToolbarDatas { get; }
        List<ItemData> ItemDatas { get; }
        ItemData GetItemDataByKey(string key);

        /// <summary>
        /// 减少工具数量
        /// </summary>
        /// <param name="key"></param>
        bool SubToolbarData(string key, int count);

        bool AddToolbarData(string key, int count);
    }

    public class ToolbarSystem : AbstractSystem, IToolbarSystem
    {
        public List<ToolbarData> ToolbarDatas { get; private set; }
        public List<ItemData> ItemDatas { get; private set; } = new List<ItemData>();

        public ItemData GetItemDataByKey(string key)
        {
            var data = ItemDatas.Find(data => data.Key == key);
            if (data != null)
            {
                return data;
            }

            return null;
        }

        public bool SubToolbarData(string key, int count)
        {
            var data = ToolbarDatas.Find(data => data.Key == key);
            if (data.Count.Value <= 0)
            {
                return false;
            }

            data.Count.Value -= count;
            return true;
        }

        public bool AddToolbarData(string key, int count)
        {
            var toolbarData = ToolbarDatas.Find(data => data.Key == key);
            if (toolbarData != null)
            {
                toolbarData.Count.Value += count;

                return true;
            }

            toolbarData = ToolbarDatas.FirstOrDefault(data => data.Count.Value == 0);

            //工具栏已满的情况
            if (toolbarData == null)
            {
                return false;
            }

            toolbarData.Key = key;
            toolbarData.Count.Value += count;


            return true;
        }

        /// <summary>
        /// 添加物品数据
        /// </summary>
        /// <param name="itemData"></param>
        /// <returns></returns>
        public ItemData AddItem(ItemData itemData)
        {
            ItemDatas.Add(itemData);

            return itemData;
        }

        protected override void OnInit()
        {
            AddToolData.AddToolDatas(this);
            AddPlantData.AddPlantDatas(this);

            ToolbarDatas = new List<ToolbarData>()
            {
                new ToolbarData("harvest", 1),
                new ToolbarData("hoe", 1),
                new ToolbarData("shower", 1),
                new ToolbarData("PotatoSeedTool", 10),
                new ToolbarData("TurnipSeedTool", 5),
                new ToolbarData(string.Empty, 0),
                new ToolbarData(string.Empty, 0),
                new ToolbarData(string.Empty, 0),
                new ToolbarData(string.Empty, 0),
            };

            var saveUtility = this.GetUtility<ISaveUtility>();

            var toolbarDatas = saveUtility.LoadString("ToolbarDatas",string.Empty);
            if (string.IsNullOrEmpty(toolbarDatas))
            {
                
            }
            else
            {
                var saveToolbarData = JsonUtility.FromJson<SaveToolbarData>(toolbarDatas);
                for (var i = 0; i < saveToolbarData.Datas.Count; i++)
                {
                    var data = ToolbarDatas[i];
                    data.Key = saveToolbarData.Datas[i].Key;
                    data.Count.Value = saveToolbarData.Datas[i].Count;
                }
            }
            
            saveUtility.AddSaveDataAction((system) =>
            {
                var saveToolbarData = new SaveToolbarData();
                foreach (var toolbarData in ToolbarDatas)
                {
                    saveToolbarData.Datas.Add(new SaveToolbarConfig(toolbarData.Key, toolbarData.Count.Value));
                }

                system.SaveString("ToolbarDatas", JsonUtility.ToJson(saveToolbarData));
            });
        }
    }
}